package home.control;

import home.control.service.AlarmCheck;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Welcome extends Activity implements OnClickListener {
	
	private Button schedulerBT;
	private Button controlPanelBT;
	private Button changeBuildingBT;
	private String TAG = "Welcome";
	private Thread alarmCheck;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        
        schedulerBT = (Button) findViewById(R.id.schedulerBT);
        schedulerBT.setOnClickListener(this);
        controlPanelBT = (Button) findViewById(R.id.controlPanelBT);
        controlPanelBT.setOnClickListener(this);
        changeBuildingBT = (Button) findViewById(R.id.changeBuildingBT);
        changeBuildingBT.setOnClickListener(this);
        
		alarmCheck = new AlarmCheck(this);
		alarmCheck.start();
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.schedulerBT:{
			Intent i = new Intent(this, Scheduler.class);
			startActivity(i);
			break;
		}
		case R.id.controlPanelBT:{
			Intent i = new Intent(this, ControlPanel.class);
			startActivity(i);
			break;
		}
		case R.id.changeBuildingBT:{
			Intent i = new Intent(this, Login.class);
			startActivity(i);
			break;
		}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	    	Log.d(TAG, "Back Pressed");
	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setMessage("Are you sure you want to exit?")
	    	       .setCancelable(false)
	    	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                moveTaskToBack(true); //Welcome.this.finish();
	    	           }
	    	       })
	    	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	                dialog.cancel();
	    	           }
	    	       });
	    	AlertDialog alert = builder.create();
	    	alert.show();
	    }
	    return true;
	}
	
	@Override
	protected void onStop() {
		super.onPause();
	}
}